package modes

import (
	"fmt"
	"time"
)

/*考试记录*/

type ExamLogRe struct {
	Id         int64  `json:"exam_id"`                         //id
	UId        int64  `json:"exam_uid" xorm:"uid"`             //学员ID
	SId        int64  `json:"exam_sid" xorm:"sid"`             //科目id
	NId        int64  `json:"exam_nid" xorm:"nid"`             //套题ID
	CId        int64  `json:"exam_cid" xorm:"cid"`             //班型ID
	Name       string `json:"exam_name" xorm:"name"`           //试卷名称
	Total      int    `json:"exam_total" xorm:"total"`         //得分总数
	Rate       int    `json:"exam_rate" xorm:"rate"`           //正确率
	HasDo      int    `json:"has_do" xorm:"has_do"`            //做了多少道题
	TotalNum   int64  `json:"total_num" xorm:"total_num"`      //可做题总数
	RightNum   int    `json:"right_num" xorm:"right_num"`      //做对多少道题
	ErrNum     int    `json:"err_num" xorm:"err_num"`          //做错多少道题
	ErNum      int    `json:"err_num" xorm:"er_num"`           //半对多少道题
	State      int    `json:"exam_state" xorm:"state"`         //状态 0 继续答题 1 交卷已完成
	UpdateAt   int64  `json:"exam_update_at" xorm:"update_at"` //更新时间
	At         int64  `json:"exam_at" xorm:"at"`               //创建时间
	TypeId     int    `json:"type_id" xorm:"type_id"`          //题型类型： 1:历年真题;2:章节练习题;3:模拟题
	TotalSocre int64  `json:"total_socre" xorm:"total_socre"`  //可做题总分
	UseTime    int64  `json:"use_time" xorm:"use_time"`        //做题使用时间
}

func (this *ExamLogRe) TableName() string {
	return "exam_logre"
}
func (this *ExamLogRe) Save() (int64, error) {
	this.At = time.Now().Unix()
	this.UpdateAt = time.Now().Unix()
	return Db(0).Insert(this)
}

func (this *ExamLogRe) Get() (bool, error) {
	return Db(0).Get(this)
}

func (this *ExamLogRe) Count(where string) (int64, error) {
	return Db(0).Where(where).Count(this)
}

func (this *ExamLogRe) GetALLNum() ([]AllNum, error) {
	allNums := make([]AllNum, 0)
	err := Db(0).Table("exam_logre").Select(" COUNT(DISTINCT uid) num,nid n_id").GroupBy("nid").Find(&allNums)
	return allNums, err
}

//去重学生id(获得多少人做过这套题)
func (this *ExamLogRe) CountDuidRecord() (int64, error) {
	return Db(0).Distinct("uid").Where("nid= %d", this.NId).Count()
}
func (this *ExamLogRe) update(where string, field string) (int64, error) {
	return Db(0).Where(where).Cols(field).Update(this)
}

func (this *ExamLogRe) Set(field string) (int64, error) {
	where := fmt.Sprintf("sid = %d and uid = %d and nid = %d", this.SId, this.UId, this.NId)
	return this.update(where, field)
}

func (this *ExamLogRe) where(fage, count, page int, where, field string) ([]ExamLogRe, error) {
	list := make([]ExamLogRe, 0)
	var err error
	if field == "" {
		field = "update_at"
	}
	if 0 == fage { // 从大到小排序
		err = Db(0).Where(where).
			Desc(field).
			Limit(count, page*count).
			Find(&list)
	} else { // 从小到大排序
		err = Db(0).Where(where).
			Asc(field).
			Limit(count, page*count).
			Find(&list)
	}
	return list, err
}
func (this *ExamLogRe) List(val *Pager) error {
	val.TName = this.TableName()
	w := val.ToWhere()
	val.Total, _ = this.Count(w)
	list, err := this.where(val.Rules, val.Count, val.Page-1, w, "")
	if err == nil {
		val.List = list
	}
	return err
}

type TopicOwn struct {
	SId   int64 `json:"sid" xorm:"sid"`       //ID
	NId   int64 `json:"nid" xorm:"nid"`       //套题ID
	OwnDo int64 `json:"t_hasdo" xorm:"owndo"` //自己做的次数
}

func (this *ExamLogRe) GetOwnList() ([]TopicOwn, error) {
	list := make([]TopicOwn, 0)
	err := Db(0).Table("exam_logre").Select("COUNT(1) owndo,nid,sid ").GroupBy("  nid,sid").Where("uid=?", this.UId).Find(&list)
	return list, err
}

func (this *ExamLogRe) GetList() ([]ExamLogRe, error) {
	list := make([]ExamLogRe, 0)
	err := Db(0).Table("exam_logre").Find(&list)
	return list, err

}
func (this *ExamLogRe) GetFirstExam() ([]ExamLogRe, error) {

	list := make([]ExamLogRe, 0)
	where := fmt.Sprintf("sid >= %d and sid< %d and uid=%d ", this.SId-(this.SId%100000), this.SId-(this.SId%100000)+100000, this.UId)
	err := Db(0).Table("exam_logre").OrderBy("update_at").Where(where).Desc("update_at").Limit(1).Find(&list)
	return list, err

}

/*SELECT sid from topic_log
GROUP BY uid,sid*/
func (this *ExamLogRe) GetELSidList() ([]int64, error) {

	list := make([]int64, 0)
	err := Db(0).Select("sid").Distinct("sid").Table("exam_logre").GroupBy(" uid,sid").Where("uid=?", this.UId).Find(&list)
	return list, err

}

func (this *ExamLogRe) UpdateById() error {
	_, e := Db(0).Table("exam_logre").Cols("has_do,total,right_num,er_num,err_num,total_num,rate").Where("id=?", this.Id).Update(this)
	return e
}
func (this *ExamLogRe) UpdateTypeId(nidList []int64, typeId int) error {
	this.TypeId = typeId
	_, e := Db(0).Table("exam_logre").Cols("type_id").In("nid", nidList).Update(this)
	return e

}

func (this *ExamLogRe) ExamLogReList(page *PageFields) (int64, []ExamLogRe, error) {
	var ques []ExamLogRe

	where := fmt.Sprintf(" uid = %d and sid >= %d and sid< %d ", this.UId, this.SId-(this.SId%100000), this.SId-(this.SId%100000)+100000)
	if this.TypeId > 0 {
		where += fmt.Sprintf(" and type_id=%d", this.TypeId)
	}
	tbl := Db(0).Table("exam_logre").Where(where)

	total, err := tbl.Clone().Count()
	if err != nil {
		return 0, nil, err
	}

	page.Page--
	if err := tbl.Desc("at").Limit(page.Pnum, page.Page*page.Pnum).Find(&ques); err != nil {
		return 0, nil, err
	}

	return total, ques, nil
}
